#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
//#include "serial.c"
//using namespace std;




//This function threshold the HSV image and create a binary image
IplImage* GetThresholdedImage(IplImage* imgHSV){
       IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
       cvInRangeS(imgHSV, cvScalar(170,160,60,0), cvScalar(180,256,255,0), imgThresh);
       return imgThresh;
}





int main(){
      CvCapture* capture =0;
      int i;
    char Data_A='A';
    char Data_B='B';

      capture = cvCaptureFromCAM(1);
      if(!capture){
            printf("Capture failure\n");
            return -1;
      }

      IplImage* frame=0;
      cvNamedWindow("Video",0);
      cvNamedWindow("Ball",0);


//wait for input character 'P' from user
    while(1)
    {
              //Wait 50mS
            int c1 = cvWaitKey(10);
            //If 'ESC' is pressed, break the loop
            if((char)c1=='P' ) break;
      }


                     //iterate through each frames of the video
      while(1){

            frame = cvQueryFrame(capture);
            if(!frame) break;

            frame=cvCloneImage(frame);
            cvSmooth(frame, frame, CV_GAUSSIAN,3,3,0,0); //smooth the original image using Gaussian kernel

            IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
            cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV
            IplImage* imgThresh = GetThresholdedImage(imgHSV);

            cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3,0,0); //smooth the binary image using Gaussian kernel



            cvShowImage("Ball", imgThresh);
            cvShowImage("Video", frame);


 IplImage* imgGrayScale=cvCreateImage(cvGetSize(imgHSV),8,1);

            cvCvtColor(frame, imgGrayScale, CV_BGR2GRAY);


       cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);
       CvSeq* contours;
        CvSeq* result;

        CvMemStorage* storage =cvCreateMemStorage(0);


/* code for tracking the shape(Ball) comes here*/

 while(contours)
 {
  //obtain a sequence of points of the countour, pointed by the variable 'countour'
  result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02,0);

  //if there are 3 vertices  in the contour and the area of the triangle is more than 100 pixels
  if(result->total==3 && fabs(cvContourArea(result, CV_WHOLE_SEQ,0))>100 )
  {
   //iterating through each point
   CvPoint *pt[3];
   for(i=0;i<3;i++){
    pt[i] = (CvPoint*)cvGetSeqElem(result, i);
   }

   //drawing lines around the triangle
   cvLine(frame, *pt[0], *pt[1], cvScalar(255,0,0,0),4,8,0);
   cvLine(frame, *pt[1], *pt[2], cvScalar(255,0,0,0),4,8,0);
   cvLine(frame, *pt[2], *pt[0], cvScalar(255,0,0,0),4,8,0);

  }

else if(result->total==4 && fabs(cvContourArea(result, CV_WHOLE_SEQ,0))>40 )
     {
         //iterating through each point
         CvPoint *pt[4];
         for(i=0;i<4;i++){
             pt[i] = (CvPoint*)cvGetSeqElem(result, i);
         }

         //drawing lines around the quadrilateral
         cvLine(frame, *pt[0], *pt[1], cvScalar(0,0,255,0),4,8,0);
         cvLine(frame, *pt[1], *pt[2], cvScalar(0,0,255,0),4,8,0);
         cvLine(frame, *pt[2], *pt[3], cvScalar(0,0,255,0),4,8,0);
         cvLine(frame, *pt[3], *pt[0], cvScalar(0,0,255,0),4,8,0);


         if(cvLine)

           {
              fprintf(stderr, "R\n");

           }
     }


  //obtain the next contour
  contours = contours->h_next;


 //show the image in which identified shapes are marked
 cvNamedWindow("Tracked",0);
 cvShowImage("Tracked",frame);


 }






          //k Send_Serial_Data(&Data_A);

            //Clean up used images
             cvReleaseMemStorage(&storage);
            cvReleaseImage(&imgHSV);
            cvReleaseImage(&imgThresh);
            cvReleaseImage(&frame);

            //Wait 50mS
            int c = cvWaitKey(10);
            //If 'ESC' is pressed, break the loop
            if((char)c==27 ) break;
      }

      cvDestroyAllWindows() ;
      cvReleaseCapture(&capture);

      return 0;
}
